package name.schooley.umarks.web;

import java.lang.Long;
import java.lang.String;
import java.util.Set;
import javax.validation.Valid;
import name.schooley.umarks.domain.Bookmark;
import name.schooley.umarks.domain.Tag;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

privileged aspect BookmarkController_Roo_Controller {
    
    @RequestMapping(value = "/bookmark", method = RequestMethod.POST)
    public String BookmarkController.create(@Valid Bookmark bookmark, BindingResult result, ModelMap modelMap) {
        if (bookmark == null) throw new IllegalArgumentException("A bookmark is required");
        if (result.hasErrors()) {
            modelMap.addAttribute("bookmark", bookmark);
            modelMap.addAttribute("tags", Tag.findAllTags());
            return "bookmark/create";
        }
        bookmark.persist();
        return "redirect:/bookmark/" + bookmark.getId();
    }
    
    @RequestMapping(value = "/bookmark/form", method = RequestMethod.GET)
    public String BookmarkController.createForm(ModelMap modelMap) {
        modelMap.addAttribute("bookmark", new Bookmark());
        modelMap.addAttribute("tags", Tag.findAllTags());
        return "bookmark/create";
    }
    
    @RequestMapping(value = "/bookmark/{id}", method = RequestMethod.GET)
    public String BookmarkController.show(@PathVariable("id") Long id, ModelMap modelMap) {
        if (id == null) throw new IllegalArgumentException("An Identifier is required");
        modelMap.addAttribute("bookmark", Bookmark.findBookmark(id));
        return "bookmark/show";
    }
    
    @RequestMapping(value = "/bookmark", method = RequestMethod.GET)
    public String BookmarkController.list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            modelMap.addAttribute("bookmarks", Bookmark.findBookmarkEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));
            float nrOfPages = (float) Bookmark.countBookmarks() / sizeNo;
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            modelMap.addAttribute("bookmarks", Bookmark.findAllBookmarks());
        }
        return "bookmark/list";
    }
    
    @RequestMapping(method = RequestMethod.PUT)
    public String BookmarkController.update(@Valid Bookmark bookmark, BindingResult result, ModelMap modelMap) {
        if (bookmark == null) throw new IllegalArgumentException("A bookmark is required");
        if (result.hasErrors()) {
            modelMap.addAttribute("bookmark", bookmark);
            modelMap.addAttribute("tags", Tag.findAllTags());
            return "bookmark/update";
        }
        bookmark.merge();
        return "redirect:/bookmark/" + bookmark.getId();
    }
    
    @RequestMapping(value = "/bookmark/{id}/form", method = RequestMethod.GET)
    public String BookmarkController.updateForm(@PathVariable("id") Long id, ModelMap modelMap) {
        if (id == null) throw new IllegalArgumentException("An Identifier is required");
        modelMap.addAttribute("bookmark", Bookmark.findBookmark(id));
        modelMap.addAttribute("tags", Tag.findAllTags());
        return "bookmark/update";
    }
    
    @RequestMapping(value = "/bookmark/{id}", method = RequestMethod.DELETE)
    public String BookmarkController.delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size) {
        if (id == null) throw new IllegalArgumentException("An Identifier is required");
        Bookmark.findBookmark(id).remove();
        return "redirect:/bookmark?page=" + ((page == null) ? "1" : page.toString()) + "&size=" + ((size == null) ? "10" : size.toString());
    }
    
    @RequestMapping(value = "find/ByTags/form", method = RequestMethod.GET)
    public String BookmarkController.findBookmarksByTagsForm(ModelMap modelMap) {
        modelMap.addAttribute("tags", Tag.findAllTags());
        return "bookmark/findBookmarksByTags";
    }
    
    @RequestMapping(value = "find/ByTags", method = RequestMethod.GET)
    public String BookmarkController.findBookmarksByTags(@RequestParam("tags") Set<Tag> tags, ModelMap modelMap) {
        if (tags == null) throw new IllegalArgumentException("A Tags is required.");
        modelMap.addAttribute("bookmarks", Bookmark.findBookmarksByTags(tags).getResultList());
        return "bookmark/list";
    }
    
}
